package com.bxs.detector.impl;

import com.bxs.detector.BaseDetector;
import com.bxs.global.JsonUtils;
import com.bxs.pojo.DangerMsg;
import com.bxs.pojo.BaseRule;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.pcap4j.core.PcapPacket;

import java.util.List;
import java.util.Map;

/**
 * @author dream
 * @title: CustomRuleDetector
 * @projectName ids_v0_2
 * @description: TODO
 */
@Slf4j
// 自定义规则检测
public class CustomRuleDetector extends BaseDetector {


    public CustomRuleDetector(){

    }

    @Override
    public DangerMsg detect(PcapPacket packet, List<BaseRule> rules) {
        log.info("CustomRuleDetector execute...");
        for(BaseRule rule : rules){
            detectProtocolFieldsPacketByRule(packet, rule);
        }
        return null;
    }

    /**
     *
     * @param packet
     * @param rule
     * @return 是否满足规则（检测到威胁）
     */
    public boolean detectProtocolFieldsPacketByRule(PcapPacket packet, BaseRule rule){
        log.info("协议字段检测...");
        Map<String, Object> protocolFields = null;
        try {
            protocolFields = JsonUtils.readFromJson(rule.getProtocolFields());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        for (Map.Entry<String, Object> entry : protocolFields.entrySet()){
            String key = entry.getKey();
            String value = (String)entry.getValue();
        }
        return false;
    }

    /**
     * 正则检测实现
     */
    public boolean detectRegexContentPacketByRUle(PcapPacket packet, BaseRule rule){
        log.info("正则检测...");

        return false;
    }
}
